/*
 *
 *  Copyright (C) 2010 Tal Eisenberg
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package fork.modbox;

import java.awt.Color;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import javax.swing.BoxLayout;
import javax.swing.JPanel;
import tal.drivechain.chain.Chain;
import tal.drivechain.gui.TextButton;
import fork.Modbox;
import java.awt.event.MouseAdapter;
import javax.swing.Box;
import tal.drivechain.chain.ChainNode;
import tal.drivechain.system.Globals;

/**
 *
 * @author eisental
 */
public class ChildrenPanel extends JPanel implements ActionListener {
    private Chain chain;
    private Color color;
    private Modbox owner;

    public ChildrenPanel(Modbox owner) {
        this.owner = owner;
        this.setBackground(Color.BLACK);
        this.setLayout(new BoxLayout(this, BoxLayout.X_AXIS));
        setColor((Color)owner.getColor());
        setFont((Font)owner.getFont());
        setChain((Chain)owner.getActualBoxChain());
    }

    public void setColor(Color color) {
        this.color = color;
        if (chain!=null)
            this.setChain(chain);
    }

    public void setChain(Chain chain) {
        this.chain = chain;
        this.removeAll();

        if (chain!=null) {
            for (ChainNode child : chain.getChildNodes().values()) {
                if (child instanceof Chain) {
                    Color xor = new Color(color.getRed()^255, color.getGreen()^255, color.getBlue()^255);
                    TextButton b = new TextButton(" " + child.getName() + " ", xor, getFont());
                    b.setActionCommand(child.getPath());
                    b.setHasBorder(false);
                    b.setNegative(true);
                    b.addActionListener(this);
                    b.addMouseListener(new MouseAdapter() {
                        @Override
                        public void mouseClicked(MouseEvent e) {
                            if (e.getButton()==MouseEvent.BUTTON3) {
                                ChildrenPanel.this.owner.getGUI().getGraph().clearSelection();
                                ChainNode node = Globals.findNode(((TextButton)e.getSource()).getActionCommand());
                                for (NodeCube c : ChildrenPanel.this.owner.getGUI().getGraph().getCubes())
                                    if (c.getChainNode()==node)
                                        c.setSelected(true);
                                ChildrenPanel.this.owner.getGUI().showNodePopup(node, e.getLocationOnScreen());
                            }
                        }
                    });
                    this.add(b);
                }
            }
        }
        this.add(Box.createHorizontalGlue());
        
        this.validateTree();
        this.repaint();
        owner.getGUI().validate();
    }

    public void actionPerformed(ActionEvent e) {
        String sChain = ((TextButton)e.getSource()).getActionCommand();
        owner.setBoxChain(sChain, null);
    }

    public void refresh() {
        setChain(chain);
    }
}